package com.chen.blog.controller;

import com.chen.blog.pojo.Category;
import com.chen.blog.service.CategoryService;
import com.chen.blog.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cgj
 * @date 1/19/25 10:37 AM
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
@Tag(name = "文章分类模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping()
    @Operation(summary = "查询所有分类")
    public Result<List<Category>> showAll(){
        List<Category> categoryList = categoryService.findAllCategory();
        return  Result.ok(categoryList);
    }
    @PostMapping("/{pageIndex}/{pageSize}")
    public Result showAllByPage(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,@RequestBody Category category){
        Map map = categoryService.findAllByPage(pageIndex, pageSize, category);
        return Result.ok(map);
    }
    @PutMapping()
    public  Result changeCategory(@RequestBody Category category){
        Integer row = categoryService.changeCategory(category);
        if(row>0){
            return  Result.ok();
        }else {
            return  Result.fail();
        }
    }
    @PostMapping()
    public Result addCategory(@RequestBody Category category){
        Integer row = categoryService.addCategory(category);
        if(row>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @DeleteMapping("/{cid}")
    public Result removeCategory(@PathVariable Integer cid){
        Integer row = categoryService.removeCategory(cid);
        if(row>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

}
